var util = require('../../utils/util.js');
const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  data: {
    choose: 1,
    active: 1,
    pageindex: 1,
    pageszie: 30,
    srctype: "筛选",
    customItem: '全部',
    multiArray: [['全部', '大类'], ['测试', '测试']],
    objectMultiArray: [
      [
        {
          id: 0,
          name: '无脊柱动物'
        },
      ], [
        {
          id: 0,
          name: '扁性动物'
        }
      ]
    ],
    multiIndex: [0, 0],
    alllist: [],
    choosetype: 1,
    vendortype:0,
    mendiantype:0,
    pageindex:1,
    pagesize:20,
    totop:false,
    showtop:false
  },
  onLoad: function (options) {
    var that = this;
    // 获取成员信息
    var datas = {};
    datas.ParentInvCID = 0;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var iii = {};
        var mularr = [{ id: 0, name: "全部" }];
        for (var i = 1; i <= res.data.data.length; i++) {
          iii.id = res.data.data[i - 1].InvCID;
          iii.name = res.data.data[i - 1].InvCName;
          mularr[i] = iii;
          iii = {};
        }
        that.smalltype(mularr[0].id, mularr);

      }
    }
    var res2 = post.getdata("/Action/ERP/GetInvClass", datas, datacall);
    var userinfo = wx.getStorageSync('userinfo');
    var newdata = wx.getStorageSync('newinfo');
    // var newdata = {};
    // if (wx.getStorageSync('newinfo')){
    //   console.log(wx.getStorageSync('newinfo'))
    //   newdata.ProName = '福建省';
    //   newdata.CityName = '厦门市';
    // }else{
    //   console.log(444)
    //   newdata = wx.getStorageSync('newinfo');
    // }
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight - 100,
          region: [
            newdata.ProName ? newdata.ProName : "福建省",
            newdata.CityName ? newdata.CityName : "厦门市",
            "全部"
          ],
          date: newdata.ProName + "/" + newdata.CityName + "/" + "全部",
        })
      },
    })
    var city = newdata.ProName + "/" + newdata.CityName + "/" + "全部";
    console.log(city);
    var isvendor = "V";
    var isven = newdata.IsVendor;
    console.log(isven)
    if (isven) {
      isvendor = "S";
    }
    that.setData({
      isvendor: isvendor
    })
    // if (that.data.vendortype == 1 && that.data.mendiantype == 0) {
    //   isvendor = "V";
    // }
    // if (that.data.mendiantype == 1 && that.data.vendortype == 0) {
    //   isvendor = "S";
    // }
    //that.search(city, '', 0, isvendor);
     that.search2(city, '', 0, isvendor);
  },
  onShow: function () {
  
  },
  scrollTopFun: function (e) {
    //console.log(e.detail);
    if (e.detail.scrollTop > 50) {//触发gotop的显示条件 
      this.setData({
        showtop: true
      });
      //console.log(this.data.scrollTop)
    } else {
      this.setData({
        showtop: false
      });
    }
  },
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: that.data.multiArray,
      multiIndex: that.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    // console.log(that.data.mularr[e.detail.value].id);
    var id = that.data.mularr[e.detail.value].id;
    switch (e.detail.column) {
      case 0:
        that.smalltype(id, that.data.mularr);
        data.multiIndex[1] = 0;
        break;
    }
    that.setData(data);
  },
  bindMultiPickerChange: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(that.data.mularrs[e.detail.value[1]].id);
    var ids = that.data.mularr[e.detail.value[0]].name;
    var srctype = '';
    if (that.data.mularrs[e.detail.value[1]].name == "全部") {
      srctype = that.data.mularr[e.detail.value[0]].name;
      ids += ";全部"
    } else {
      srctype = that.data.mularrs[e.detail.value[1]].name;
      ids += ";" + that.data.mularrs[e.detail.value[1]].name;
    }
    console.log(ids);
    that.setData({
      multiIndex: e.detail.value,
      srctype: srctype,
      ids: ids
    })
    var city = that.data.date;
    var isvendor = that.data.isvendor;
    // if (that.data.vendortype == 1 && that.data.mendiantype == 0) {
    //   isvendor = "V";
    // }
    // if (that.data.mendiantype == 1 && that.data.vendortype == 0) {
    //   isvendor = "S";
    // }
    that.search2(city, ids, 0, isvendor);
  },
  //地区获取
  bindRegionChange: function (e) {
    var that = this;
    var citys = e.detail.value;
    that.setData({
      region: e.detail.value,
      date: citys[0] + '/' + citys[1]+'/'+citys[2]
    })
    var isvendor = that.data.isvendor;
    // if (that.data.vendortype == 1 && that.data.mendiantype == 0){
    //   isvendor="V";
    // }
    // if (that.data.mendiantype == 1 && that.data.vendortype == 0){
    //   isvendor = "S";
    // }
    that.search2(citys[0] + '/' + citys[1]+'/'+citys[2], that.data.ids, 0, isvendor)
  },
  smalltype: function (id, arr) {
    var that = this;
    // 获取成员信息
    var datas = {};
    datas.ParentInvCID = id;
    var datacall = function (res) {
      console.log(res);
      // return;
      if (res.data.data.length != 0) {
        var iii = {};
        var mularrs = [];
        mularrs[0] = { id: -1, name: "全部" }
        for (var i = 1; i <= res.data.data.length; i++) {
          iii.id = res.data.data[i - 1].InvCID;
          iii.name = res.data.data[i - 1].InvCName;
          mularrs[i] = iii;
          iii = {};
        }

        that.setData({
          mularrs: mularrs,
          mularr: arr,
          objectMultiArray: [arr, mularrs]
        });
        console.log(mularrs);
        return mularrs;
      }
    }
    var res2 = post.getdata("/Action/ERP/GetInvClass", datas, datacall);
  },
  searchstore: function (clear=0) {
    var that = this;
    // 获取成员信息
    var city = that.data.date;
    var ids = that.data.ids ? that.data.ids : '';
    var isvendor = that.data.isvendor;
    // if (that.data.vendortype == 1 && that.data.mendiantype == 0) {
    //   isvendor = "V";
    // }
    // if (that.data.mendiantype == 1 && that.data.vendortype == 0) {
    //   isvendor = "S";
    // }
    that.search2(city, ids, 0, isvendor,clear)
  },
  getkey: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  search: function (city, ids, more = 0, isvendor = 0,clear=0) {
    var that = this;
    var datas = {};
    if(clear==1){
      var key = '';
    }else{
      var key = that.data.key ? that.data.key : '';
    }
    
    datas.Type = isvendor;
    datas.Search = "%" + key + "%";
    datas.MebID = wx.getStorageSync('userinfo').MebID;
    if (city) {
      var proname = "%" + city.split('/')[0] + "%";
      var cityname = city.split('/')[1];
      var areaname = city.split('/')[2];
      if (cityname == '全部') {
        datas.CityName = "%%";
      } else {
        datas.CityName = "%" + cityname + "%";
      }
      if (areaname == '全部') {
        datas.AreaName = "%%";
      } else {
        datas.AreaName = "%" + areaname + "%";
      }
      datas.ProName = proname;
      // datas.AreaName = '%%';
    }
    if (ids) {
      var idsarr = ids.split(';');
      if (idsarr[0] == "全部") {
        datas.RootClass = '%%'
      } else {
        datas.RootClass = "%" + idsarr[0] + "%"
      }
      if (idsarr[1] == "全部") {
        datas.LastClass = '%%'
      } else {
        datas.LastClass = "%" + idsarr[1] + "%"
      }

    } else {
      datas.RootClass = '%%';
      datas.LastClass = '%%';
    }
    console.log(datas);
    var datacall = function (res) {
      console.log(res);
      var datas = res.data.data
      if (datas.length > 0) {
        for (var i = 0; i < datas.length; i++) {
          // datas[i].isshou = 0;
          if (datas[i].MebName && datas[i].MebName.length > 3) {
            datas[i].MebName = datas[i].MebName.substring(0, 3) + "...";
          }
          if (datas[i].Brands && datas[i].Brands.length > 12) {
            datas[i].Brands = datas[i].Brands.substring(0, 12) + "...";
          }
          if (datas[i].AccName && datas[i].AccName.length > 12) {
            datas[i].AccName = datas[i].AccName.substring(0, 11) + "...";
          }
        }
      }

      that.setData({
        alllist: datas
      })
    }
    var res2 = post.getdata("/Action/PK01/FindPartner", datas, datacall);
  },
  search2: function (city, ids, more = 0, isvendor = 0, clear = 0) {
    var that = this;
    wx.showLoading({
      title: '正在加载...',
    })
    var datas = {};
    if (clear == 1) {
      var key = '';
    } else {
      var key = that.data.key ? that.data.key : '';
    }
    datas.pagesize=20;
    datas.pageindex=1;
    datas.summary=1;
    var where = '';
    if(isvendor){
      where += "MebID <> " + wx.getStorageSync('userinfo').MebID + " and MyMebID = " + wx.getStorageSync('userinfo').MebID;
    }
    
    datas.Type = isvendor;
    datas.Search = "%" + key + "%";
    datas.MebID = wx.getStorageSync('userinfo').MebID;
    if (city) {
      var proname = "%" + city.split('/')[0] + "%";
      var cityname = city.split('/')[1];
      var areaname = city.split('/')[2];
      if (cityname == '全部') {
        datas.CityName = "%%";
      } else {
        datas.CityName = "%" + cityname + "%";
      }
      if (areaname == '全部') {
        datas.AreaName = "%%";
      } else {
        datas.AreaName = "%" + areaname + "%";
      }
      datas.ProName = proname;
    }
    if (ids) {
      var idsarr = ids.split(';');
      if (idsarr[0] == "全部") {
        datas.RootClass = '%%'
      } else {
        datas.RootClass = "%" + idsarr[0] + "%"
      }
      if (idsarr[1] == "全部") {
        datas.LastClass = '%%'
      } else {
        datas.LastClass = "%" + idsarr[1] + "%"
      }
    } else {
      datas.RootClass = '%%';
      datas.LastClass = '%%';
    }
    console.log(datas);
    var datacall = function (res) {
      wx.hideLoading()
      console.log(res);
      var datas = res.data.data
      if (datas.length > 0) {
        for (var i = 0; i < datas.length; i++) {
          // datas[i].isshou = 0;
          if (datas[i].MebName && datas[i].MebName.length > 3) {
            datas[i].MebName = datas[i].MebName.substring(0, 3) + "...";
          }
          if (datas[i].Brands && datas[i].Brands.length > 12) {
            datas[i].Brands = datas[i].Brands.substring(0, 12) + "...";
          }
          if (datas[i].DepName && datas[i].DepName.length > 12) {
            datas[i].DepName = datas[i].DepName.substring(0, 11) + "...";
          }
        }
      }
      that.setData({
        alllist: datas,
        pageindex:1,
        pagecount:res.data.pagecount
      })
    }
    var res2 = post.getdata("/BO/V_FindPartner/Query", datas, datacall);
  },
  newdata: function () {
    var that = this;
    if (that.data.pagecount == that.data.pageindex){
      return;
    }
    wx.showLoading({
      title: '正在加载...',
    })
    var datas = {};
    var key = that.data.key ? that.data.key : '';
    datas.pagesize = 20;
    datas.pageindex = that.data.pageindex+1;
    datas.summary = 1;
    var isvendor = that.data.isvendor;
    datas.Type = isvendor;
    datas.Search = "%" + key + "%";
    datas.MebID = wx.getStorageSync('userinfo').MebID;
    var city = that.data.date;
    if (city) {
      var proname = "%" + city.split('/')[0] + "%";
      var cityname = city.split('/')[1];
      var areaname = city.split('/')[2];
      if (cityname == '全部') {
        datas.CityName = "%%";
      } else {
        datas.CityName = "%" + cityname + "%";
      }
      if (areaname == '全部') {
        datas.AreaName = "%%";
      } else {
        datas.AreaName = "%" + areaname + "%";
      }
      datas.ProName = proname;
    }
    var ids = that.data.ids ? that.data.ids : '';
    if (ids) {
      var idsarr = ids.split(';');
      if (idsarr[0] == "全部") {
        datas.RootClass = '%%'
      } else {
        datas.RootClass = "%" + idsarr[0] + "%"
      }
      if (idsarr[1] == "全部") {
        datas.LastClass = '%%'
      } else {
        datas.LastClass = "%" + idsarr[1] + "%"
      }
    } else {
      datas.RootClass = '%%';
      datas.LastClass = '%%';
    }
    console.log(datas);
    var datacall = function (res) {
      
      console.log(res);
      var datas = res.data.data;
      var alllist=that.data.alllist;
      if (datas.length > 0) {
        for (var i = 0; i < datas.length; i++) {
          // datas[i].isshou = 0;
          if (datas[i].MebName && datas[i].MebName.length > 3) {
            datas[i].MebName = datas[i].MebName.substring(0, 3) + "...";
          }
          if (datas[i].Brands && datas[i].Brands.length > 12) {
            datas[i].Brands = datas[i].Brands.substring(0, 12) + "...";
          }
          if (datas[i].DepName && datas[i].DepName.length > 12) {
            datas[i].DepName = datas[i].DepName.substring(0, 11) + "...";
          }
          alllist.push(res.data.data[i]);
        }
      }
      that.setData({
        alllist: alllist,
        pageindex: that.data.pageindex + 1
      })
      wx.hideLoading()
    }
    var res2 = post.getdata("/BO/V_FindPartner/Query", datas, datacall);
  },
  addfriend: function (e) {
    console.log(e);
    var frid = e.currentTarget.dataset.frid;
    var index = e.currentTarget.dataset.index;
    var myid = wx.getStorageSync('userinfo').MebID;
    var that = this;
    var alldatas = that.data.alllist;
    alldatas[index].IsFriend = 1;
    that.setData({
      alllist: alldatas
    })
    // 添加好友
    var datas = {};
    var data = {};
    datas.MyMebID = myid;
    datas.FrMebID = frid;
    datas.DepID = e.currentTarget.dataset.accid;
    console.log(datas);
    data.data = JSON.stringify(datas);
    console.log(datas);
    var datacall = function (res) {
      console.log(res);
      if (res.data.code == 0) {
        console.log(alldatas);
        alldatas[index].RelID = res.data.data.RelID;
        that.setData({
          alllist: alldatas
        })
        console.log(res.data.code)
        return;
      }
    }
    var res2 = post.getdata("/BO/BA_Friends/Add", data, datacall);
  },
  removefriend: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var relid = e.currentTarget.dataset.relid;
    var that = this;
    var datas = that.data.alllist;
    datas[index].IsFriend = 0;
    that.setData({
      alllist: datas
    })
    // 添加好友
    var datas = {};
    datas.RelID = relid;
    console.log(datas);
    var datacall = function (res) {
      console.log(res);
      if (res.data.code == 0) {
        console.log(res.data.code)
        return;
      }
    }
    var res2 = post.getdata("/BO/BA_Friends/Delete", datas, datacall);
  },
  toinfo: function (e) {
    console.log(e)
    var that = this;
    var mebid = e.currentTarget.dataset.mebid;
    var isfriend = e.currentTarget.dataset.hasadd;
    var relid = e.currentTarget.dataset.relid;
    var accid = e.currentTarget.dataset.accid;
    wx.navigateTo({
      url: '/pages/findinfo/findinfo?mebid=' + mebid + '&isfriend=' + isfriend + '&relid=' + relid+'&accid='+accid,
    })
  },
  clearall:function(){
    var that = this;
    that.setData({
      key:''
    });
    that.searchstore(1)
  }
})